(function() {
    angular.module('app').factory('addMonitorService', addMonitorService);

    addMonitorService.$inject = ['util', 'atomicNotifyService'];

    function addMonitorService(util, atomicNotifyService) {
        return {
            add: _add
        }

        function _add(config, type) {

            switch (type) {
                case 'shop':
                    _addShopMonitor(config);
                    break;
                case 'brand':
                    _addBrandMonitor(config);
                    break;
                case 'product':
                    _addProductMonitor(config);
                    break;
                default:
                    console.log('没有这种类型监控=---' + type);
                    break;
            }

        }

        function _addShopMonitor(shop) {
            //是否已经监控中
            if (!shop.collectionStatus) {
                //添加监控
                util.$get({
                    url: '/marketconditions/collection/addCollection.htm',
                    data: {
                        targetId: shop.userId,
                        type: 'shop'
                    }
                }).then(function(resp) {
                    var message = '开始监控-' + shop.shopName;
                    atomicNotifyService.success(message);
                    shop.collectionStatus = 'collection';

                }, function(err) {
                    if (err.message == 'shop count limit') {
                        atomicNotifyService.warning('抱歉,您的店铺监控数已达上限!');
                    } else {
                        atomicNotifyService.info('抱歉,该店铺监控失败!');
                    }
                });


            } else {


                util.$get({
                    url: '/marketconditions/collection/deleteCollections.htm',
                    data: {
                        targetId: shop.userId,
                        type: 'shop'
                    }
                }).then(function(resp) {
                    shop.collectionStatus = null;
                    var message = '取消监控-' + shop.shopName;
                    atomicNotifyService.info(message);
                }, function(err) {
                    var message = '取消监控-' + shop.shopName + '失败!';
                    atomicNotifyService.warning(message);
                });
            }
        }

        function _addBrandMonitor(brand) {
            //是否已经监控中
            if (!brand.collectionStatus) {
                //添加监控
                util.$get({
                    url: '/marketconditions/collection/addCollection.htm',
                    data: {
                        targetId: brand.brandName,
                        type: 'brand'
                    }
                }).then(function(resp) {
                    var message = '开始监控-' + brand.brandName;
                    atomicNotifyService.success(message);
                    brand.collectionStatus = 'collection';

                }, function(err) {
                    if (err.message == 'brand count limit') {
                        atomicNotifyService.warning('抱歉,您的品牌监控数已达上限!');
                    } else {
                        atomicNotifyService.info('抱歉,该品牌监控失败!');
                    }
                });


            } else {


                util.$get({
                    url: '/marketconditions/collection/deleteCollections.htm',
                    data: {
                        targetId: brand.brandName,
                        type: 'brand'
                    }
                }).then(function(resp) {
                    brand.collectionStatus = null;
                    var message = '取消监控-' + brand.brandName;
                    atomicNotifyService.info(message);
                }, function(err) {
                    var message = '取消监控-' + brand.brandName + '失败!';
                    atomicNotifyService.warning(message);
                });
            }
        }

        function _addProductMonitor(product) {
            //是否已经监控中
            if (!product.collectionStatus) {
                //添加监控
                util.$get({
                    url: '/marketconditions/collection/addCollection.htm',
                    data: {
                        targetId: product.numberId,
                        type: 'product'
                    }
                }).then(function(resp) {
                    var message = '开始监控-' + product.title;
                    atomicNotifyService.success(message);
                    product.collectionStatus = 'collection';

                }, function(err) {
                    if (err.message == 'product count limit') {
                        atomicNotifyService.warning('抱歉,您的商品监控数已达上限!');
                    } else {
                        atomicNotifyService.info('抱歉,该商品监控失败!');
                    }
                });


            } else {


                util.$get({
                    url: '/marketconditions/collection/deleteCollections.htm',
                    data: {
                        targetId: product.numberId,
                        type: 'product'
                    }
                }).then(function(resp) {
                    product.collectionStatus = null;
                    var message = '取消监控-' + product.title;
                    atomicNotifyService.info(message);
                }, function(err) {
                    var message = '取消监控-' + product.title + '失败!';
                    atomicNotifyService.warning(message);
                });
            }
        }


    }
})();
